package com.food.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.resource.PathResourceResolver;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
    
    @Value("${file.upload.path}")
    private String uploadPath;
    
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 静态资源映射
        registry.addResourceHandler("/static/**")
                .addResourceLocations(
                    "classpath:/static/",  // 类路径下的静态资源
                    "file:src/main/resources/static/"  // 文件系统中的静态资源
                )
                .setCachePeriod(3600)
                .resourceChain(false);
        
        // 食物图片专门的映射
        registry.addResourceHandler("/images/foods/**")
                .addResourceLocations(
                    "classpath:/static/images/foods/",
                    "file:src/main/resources/static/images/foods/"
                )
                .setCachePeriod(3600)
                .resourceChain(false);
        
        // 头像图片专门的映射
        registry.addResourceHandler("/images/avatar/**")
                .addResourceLocations(
                    "classpath:/static/images/avatar/",
                    "file:src/main/resources/static/images/avatar/"
                )
                .setCachePeriod(3600)
                .resourceChain(false);
        
        // 添加食谱图片的映射
        registry.addResourceHandler("/images/recipes/**")
                .addResourceLocations(
                    "classpath:/static/images/recipes/",
                    "file:src/main/resources/static/images/recipes/"
                )
                .setCachePeriod(3600)
                .resourceChain(false);
        
        // 上传文件映射
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:" + uploadPath + "/")
                .setCachePeriod(3600)
                .resourceChain(false);

        // Knife4j 资源映射
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
    
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("http://localhost:8081")
                .allowedMethods("GET", "POST", "PUT", "DELETE", "OPTIONS")
                .allowedHeaders("*")
                .allowCredentials(true)
                .maxAge(3600);
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.addAllowedOrigin("http://localhost:8081");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        config.setMaxAge(3600L);
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
    }
} 